var date_now = new Date();
var curdate=date_now
var daydex=date_now.getDate()-1
Page({
  /**
   * 页面的初始数据
   */
  data: {
    biology:{
      startday:'',
      endday:'',
      nowday:'',
      flower:'',
      color:'',
      pain:'',
      temper:'',
      mood:'',
      habit:'',
      openid:''
    },
    date:[[31,28,31,30,31,30,31,31,30,31,30,31],
          [31,29,31,30,31,30,31,31,30,31,30,31]],
    curday:"",
    days:0, //本月天数
    curmonth:'',//当前月份
    weeklist:['周日','周一','周二','周三','周四','周五','周六'],
    weekday:"",
    bgYes:"lightgray",
    bgNo:"#fa822c",
    daydex:'',//点击的日期索引
    startday:32,
    endday:-1,
    iscame:true,
    isover:false,
    emotion:[
      {
        src:'../../images/icons/超开心.png',
        description:'超开心'
      },
      {
        src:'../../images/icons/开心.png',
        description:'开心'
      },
      {
        src:'../../images/icons/一般.png',
        description:'一般'
      },
      {
        src:'../../images/icons/不开心.png',
        description:'不开心'
      }
    ],
    habits:[
      {
        src:'../../images/icons/便便.png',
        description:'便便'
      },
      {
        src:'../../images/icons/饮食习惯.png',
        description:'按时吃饭'
      },
      {
        src:'../../images/icons/运动.png',
        description:'运动'
      },
      {
        src:'../../images/icons/喝水.png',
        description:'喝水'
      },
      {
        src:'../../images/icons/水果.png',
        description:'水果'
      }
  ]
  },
  clickTemperature(){
    wx.showModal({
      title: '体温',
      editable:true,
      placeholderText:'体温的格式如:36℃',
      success:(res)=>{
        this.setData({
          //这里还得改一下,温度要显示在页面上
          ['biology.temper']:res.content
        })
      }
    })
  },
  clickPain(){
    wx.showActionSheet({
      itemList: ['完全不痛','轻微痛','比较痛','非常痛','超级痛'],
      success:(res)=>{
        switch(res.tapIndex){
          case 0:
            this.setData({
              ['biology.pain']:'完全不痛'
            })
            break
          case 1:
            this.setData({
              ['biology.pain']:'轻微痛'
            })
            break
          case 2:
            this.setData({
              ['biology.pain']:'比较痛'
            })
            break
          case 3:
            this.setData({
              ['biology.pain']:'非常痛'
            })
            break
          case 4:
            this.setData({
              ['biology.pain']:'超级痛'
            })
            break
        }
      }
    })
  },
  clickColor(){
    wx.showActionSheet({
      itemList: ['浅红色','鲜红色','深红色','褐色','黑色'],
      success:(res)=>{
        switch(res.tapIndex){
          case 0:
            this.setData({
              ['biology.color']:'浅红色'
            })
            break
          case 1:
            this.setData({
              ['biology.color']:'鲜红色'
            })
            break
          case 2:
            this.setData({
              ['biology.color']:'深红色'
            })
            break
          case 3:
            this.setData({
              ['biology.color']:'褐色'
            })
            break
          case 4:
            this.setData({
              ['biology.color']:'黑色'
            })
            break
        }
      }
    })
  },
  clickQuantity(){
    wx.showActionSheet({
      itemList: ['非常少量','少量','中量','大量','非常大量'],
      success:(res)=>{
        switch(res.tapIndex){
          case 0:
            this.setData({
              ['biology.flower']:'非常少量'
            })
            break
          case 1:
            this.setData({
              ['biology.flower']:'少量'
            })
            break
          case 2:
            this.setData({
              ['biology.flower']:'中量'
            })
            break
          case 3:
            this.setData({
              ['biology.flower']:'大量'
            })
            break
          case 4:
            this.setData({
              ['biology.flower']:'非常大量'
            })
            break
        }
      }
    })
  },
  clickPlan(){
    wx.navigateTo({
      url: '../plan/index',
    })
  },
  clickHabits(e){
    var description=e.target.dataset.description
    if(description=='水果'){
      this.setData({
        'habits[4].src':'../../images/icons/水果_active.png',
        ['biology.habit']:'水果'
      })
    }else if(description=='便便'){
      this.setData({
        'habits[0].src':'../../images/icons/便便_active.png',
        ['biology.habit']:'便便'
      })
    }else if(description=='喝水'){
      this.setData({
        'habits[3].src':'../../images/icons/喝水_active.png',
        ['biology.habit']:'喝水'
      })
    }else if(description=='运动'){
      this.setData({
        'habits[2].src':'../../images/icons/运动_active.png',
        ['biology.habit']:'运动'
      })
    }else{
      this.setData({
        'habits[1].src':'../../images/icons/饮食习惯_active.png',
        ['biology.habit']:'按时吃饭'
      })
    }
  },
  clickEmotion(e){
    var description=e.target.dataset.description
    if(description=='超开心'){
      this.setData({
        'emotion[0].src':'../../images/icons/超开心_active.png',
        ['biology.mood']:'超开心'
      })
    }else if(description=='开心'){
      this.setData({
        'emotion[1].src':'../../images/icons/开心_active.png',
        ['biology.mood']:'开心'
      })
    }else if(description=='一般'){
      this.setData({
        'emotion[2].src':'../../images/icons/一般_active.png',
        ['biology.mood']:'一般'
      })
    }else{
      this.setData({
        'emotion[3].src':'../../images/icons/不开心_active.png',
        ['biology.mood']:'不开心'
      })
    }
  },
  clickDiary(){
    wx.navigateTo({
      url: '../diary/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCalendar(curdate)
    var year=curdate.getFullYear()
    var month=curdate.getMonth()+1
    var day=curdate.getDate()
    this.setData({
      ['biology.nowday']:year+'年'+month+'月'+day+'日'
    })
  },
  //以下两个点击事件还没推送
  clickYes(){
    this.setData({
      bgNo:"lightgray",
      bgYes:"#fa822c"
    })
    if(this.data.iscame){
      this.setData({
        startday:daydex,
        ['biology.startday']:daydex
      })
    }else{
      this.setData({
        endday:daydex,
        ['biology.endday']:daydex,
        iscame:true,
        isover:false
      })
    }
  },
  clickNo(){
    this.setData({
      bgNo:"#fa822c",
      bgYes:"lightgray"
    })
    if(this.data.iscame){
      this.setData({
        startday:32,
        ['biology.startday']:32
      })
    }else{
      this.setData({
        endday:-1,
        ['biology.endday']:-1
      })
    }
  },
  clickDay(e){
    daydex=e.currentTarget.dataset.index
    this.setData({
      daydex
    })
    if(this.data.startday!=32&&daydex!=this.data.startday&&daydex!=this.data.endday){
      this.setData({
        iscame:false,
        isover:true,
        bgYes:"lightgray",
        bgNo:"#fa822c"
      })
    }else if(daydex==this.data.startday){
      this.setData({
        iscame:true,
        isover:false,
        bgYes:"#fa822c",
        bgNo:"lightgrey"
      })
    }else if(daydex==this.data.endday){
      this.setData({
        iscame:false,
        isover:true,
        bgYes:"#fa822c",
        bgNo:"lightgrey"
      })
    }
  },
  clickLeft(){
    var timestamp=Date.parse(curdate)/1000
    var yesterday_stamp=timestamp-curdate.getDate()*24*60*60-10
    var date=new Date(yesterday_stamp*1000)
    curdate=date
    this.getCalendar(curdate)
  },
  clickRight(){
    var timestamp=Date.parse(curdate)/1000
    var tomorrow_stamp=timestamp+(this.data.days-curdate.getDate()+1)*24*60*60
    var date=new Date(tomorrow_stamp*1000)
    curdate=date
    this.getCalendar(curdate)
  },
  getCalendar(curdate){
    var year=curdate.getFullYear();
    var month=curdate.getMonth();
    var date_first=new Date(year,month,1);
    var day=date_first.getDay();
    this.setData({
      daydex,
      weekday:day%7,
      curmonth:month+1
    })
    if(date_now.getFullYear()==year&&date_now.getMonth()==month){
      this.setData({
        curday:date_now.getDate(),
      })
    }else{
      this.setData({
        curday:-1
      })
    }
    if((year%4==0&&year%100!=0)||year%400==0){
      this.setData({
        days:this.data.date[1][curdate.getMonth()]
      })
    }else{
      this.setData({
        days:this.data.date[0][curdate.getMonth()]
      })
    }
    if(daydex>this.data.days-1){
      daydex=this.data.days-1
      this.setData({
        daydex
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})